package com.itheima.edy.info.manager.server;

import com.itheima.edy.info.manager.dao.StudentDao;
import com.itheima.edy.info.manager.domain.Student;

public class StudentServer {
    //创建库管StudentDao
    private static StudentDao studentDao = new StudentDao();

    public static Student[] findAllStudent() {
        //调用库管对象的findAllStudent 获取学生对象数组
        Student[] allstudents = studentDao.findAllStudent();
        //判断数组中是否有学生信息（有：返回地址，没有：返回null）
        //数组中只要存在一个不是null的元素，就代表有学生信息
        boolean flag = false;
        for (int i = 0; i < allstudents.length; i++) {
            Student student = allstudents[i];
            if (student != null) {
                flag = true;
                break;
            }
            }
        if (flag) {
            //有信息
            return allstudents;
        } else {
            //没有信息
            return null;
        }
    }

    public boolean addStudent(Student stu) {

        //将学生对象传给库管中的addStudent
        //将返回的boolean类型结果，返还给StudentController
         return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
         //调用库管的方法，把学生数组传回来（获取存储学生对象的数组）
         Student[]stu = studentDao.findAllStudent();
         //假设id在数组中不存在
         boolean exists = false;
         //遍历数组获取每一个学生对象，判断学号是否存在
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            //判断条件是 学生数组不为空并且学号不存在
            if(student != null && student.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public void deleteStudentById(String delId) {
          studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao. updateStudent(updateId,newStu);
    }
}
